const models = require('../db/db');
const express = require('express');
const router = express.Router();
const mysql = require('mysql');
const $sql = require('../db/sqlMap');

const conn = mysql.createConnection(models.mysql);
conn.connect();
// 查询全部
router.post('/findAll',(req,res)=>{
	const params = req.body;
	const sel= $sql.order.select;	
	conn.query(sel,params.id,(error, results)=>{
		if (error) {
			throw error;
		}
		if (results!=null) {
			res.send(results); 
		} 
	})
});

// 删除接口
router.post('/delOrder',(req,res)=>{
	const user = req.body;
	const sel_email = $sql.order.delete + " where order_id = '" + user.order_id + "'";
	conn.query(sel_email,user.order_id, (error, results)=>{
		if (error) {
			throw error;
		}else{
			res.send({msg:'ok'}); 
		}
		
	})
});

// 加入收藏接口
router.post('/addOrder', (req, res) => {
	const params = req.body;
	const add_sql = $sql.order.add;
	for (let i = 0; i < params.order.length; i++) {
		conn.query(add_sql, [params.order_no, params.order[i].cart_img, params.order[i].cart_name,params.order[i].cart_price, params.order[i].cart_num, params.confirmAddress_id,params.goods_id,params.id], (err, rst) => {
			if (err) {
				console.log(err);
			}
			if(rst!=null){
				res.send({msg:'0'});
			}
		});	
		return;
	}
});


module.exports = router;